import Vue from 'vue'
import Vuex from 'vuex'
import {getHeader} from "../utils/api";

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    username:'',
    usermingzi:'',
    userid:'',
    useryue:'',
    userlxfs:'',
    userqx:'',
    usreimg:'',
    userdrsj:'',
    dlzt:true,
    useremail:'',
    sign:true, //能否签到
    userlv:0, //用户等级
  },
  mutations: {
    setUserName(state,v){
      state.username=v;
      state.dlzt=false
    },
    setUserMingzi(state,v){
      state.usermingzi=v;
    },
    setUserId(state,v){
      state.userid=v;
    },
    setUseryue(state,v){
      state.useryue=v;
    },
    setUserlxfs(state,v){
      state.userlxfs=v;
    },
    setUserqx(state,v){
      state.userqx=v;
    },
    setUserimg(state,v){
      state.usreimg=v;
    },
    setUseremail(state,v){
      state.useremail=v;
    },
    setUserLv(state,v){
      state.userlv=v;
    },
    setDlzt(state){
      state.dlzt=true;
      state.username="";
      state.usermingzi="";
      state.userid="";
      state.useryue="";
      state.userlxfs="";
      state.userqx="";
      state.usreimg="";
      state.sign=true;
      state.userlv=0;
    },
    setSign(state,v){
      state.sign=v;
    }
  },
  actions: {
    createHeader(context){
      return new Promise((resolve,reject)=>{
        getHeader().then(response=>{
          if(response.status===200){
            context.commit('setUserMingzi',response.username2);
            context.commit('setUserqx',response.userqx);
            context.commit('setUserName',response.username);
            context.commit('setUserId',response.userid);
            context.commit('setUseryue',response.useryue);
            context.commit('setUserlxfs',response.userlxfs);
            context.commit('setUseremail',response.email || '暂未绑定')
            context.commit('setSign',response.sign)
            context.commit('setUserLv',response.userLv)
            if(response.touxiang){
              context.commit('setUserimg',response.touxiang.replace(/public/, 'http://cloud.pinggai8.com'));
            }
            resolve("ok");
          }else{
            context.commit('setDlzt');
            resolve("no");
          }
        }).catch(error=>{
          console.log(error);
          reject(error);
        })
      })
    },
  },
  modules: {
  }
})
